package com.imay.live.view.custom;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.imay.live.R;
import com.imay.live.heartlikeslib.hearthikesview.HeartLikeView;
import com.imay.live.utils.AnimationUtil;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/7/26.
 */

public class MusicPlayView extends RelativeLayout {

    private ImageView iv_play_bgm;
    private HeartLikeView fl_note_container_bgm;
    private ObjectAnimator rotationAnim;

    public MusicPlayView(Context context) {
        super(context);
        init(context);
    }

    public MusicPlayView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MusicPlayView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }

    private void init(Context context) {
        inflate(context, R.layout.view_music_play, this);
        iv_play_bgm = (ImageView) findViewById(R.id.iv_play_bgm);
        fl_note_container_bgm = (HeartLikeView) findViewById(R.id.fl_note_container_bgm);
        rotationAnim = AnimationUtil.getRotationInAnimation(iv_play_bgm, 0f, 359, 3000);
    }

    public void startRotationAnim() {
        if (rotationAnim != null) {
            rotationAnim.start();
            this.postDelayed(mRunnable, 300);
        }
    }

    private int localX;
    private int dx;
    private Rect rect;
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            postDelayed(this, 300);
            if (localX == 0) {
                dx = fl_note_container_bgm.getWidth() - iv_play_bgm.getWidth();
                localX = (iv_play_bgm.getRight() - iv_play_bgm.getLeft()) / 2;
                rect = new Rect();
                iv_play_bgm.getHitRect(rect);
            }
            fl_note_container_bgm.showHeartView(localX + dx, rect.exactCenterY());
        }
    };

    public void pauseRotationAnim() {
        this.removeCallbacks(mRunnable);
        if (rotationAnim != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                rotationAnim.pause();
            } else {
                rotationAnim.cancel();
            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        fl_note_container_bgm.release();
        this.removeCallbacks(mRunnable);
        super.onDetachedFromWindow();
    }
}
